package com.jy.rhin.facade.infra.controller.document;

import com.jy.framework.base.entity.Result;
import com.jy.rhin.application.infra.DocumentApplicationService;
import com.jy.rhin.domain.document.validation.IDocumentValidataResultService;
import com.jy.rhin.infra.repo.document.content.query.DocumentContentQueryHandler;
import com.jy.rhin.infra.repo.document.submission.query.DocumentSubmissionQueryHandler;
import com.jy.rhin.sdk.dto.document.*;
import com.jy.rhin.sdk.dto.document.query.*;
import com.jy.rhin.sdk.feign.client.DocumentFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author dhl
 * @date 2022/5/31
 * @Description
 */
@RestController
@RequestMapping("/infra/document")
@Api(tags = "互联互通文档api")
@Slf4j
public class DocumentInfraController implements DocumentFeign {
    @Resource
    private DocumentContentQueryHandler documentContentQueryHandler;
    @Resource
    private DocumentSubmissionQueryHandler documentSubmissionQueryHandler;
    @Resource
    private DocumentApplicationService documentApplicationService;
    @Autowired
    private IDocumentValidataResultService documentValidataResultServvice;
    @Override
    @ApiOperation("文档提交")
    @PutMapping("/documentProvide")
    @CacheEvict(value = {"documentSetTotal", "documentEntryTotal", "documentEntryTotalByDay", "documentEntryTotalGroupType"}, allEntries = true)
    public Result<DocumentProvideAndRegisterDto> documentProvide(@RequestBody DocumentProvideAndRegisterDto dto) {
        return Result.success(documentApplicationService.documentProvide(dto));
    }

    @Override
    @ApiOperation("文档注册")
    @PutMapping("/documentRegister")
    @CacheEvict(value = {"documentSetTotal", "documentEntryTotal", "documentEntryTotalByDay", "documentEntryTotalGroupType"}, allEntries = true)
    public Result<Long> documentRegister(@RequestBody DocumentSubmissionSetDto submissionSet) {
        return Result.success(documentApplicationService.documentRegister(submissionSet));
    }

    @Override
    @ApiOperation("文档元数据更新")
    @PutMapping("/documentUpdate")
    @CacheEvict(value = {"documentSetTotal", "documentEntryTotal", "documentEntryTotalByDay", "documentEntryTotalGroupType"}, allEntries = true)
    public Result<Long> documentUpdate(@RequestBody DocumentSubmissionSetDto submissionSet) {
        return Result.success(documentApplicationService.documentUpdate(submissionSet));
    }

    @Override
    @ApiOperation("C.2.1 查询文档（FindDocuments）")
    @PostMapping("/findDocuments")
    public Result<DocumentResultDto<DocumentEntryDto>> findDocuments(@RequestBody FindDocumentsDto dto) {
        return Result.success(documentSubmissionQueryHandler.findDocuments(dto));
    }

    @Override
    @ApiOperation("C.2.2 查询文档提交集（FindSubmissionSets）")
    @PostMapping("/findSubmissionSets")
    public Result<DocumentResultDto<DocumentSubmissionSetDto>> findSubmissionSets(@RequestBody FindSubmissionSetsDto dto) {
        return Result.success(documentSubmissionQueryHandler.findSubmissionSets(dto));
    }

    @Override
    @ApiOperation("C.2.3 查询文件夹（FindFolders）")
    @PostMapping("/findFolders")
    public Result<DocumentResultDto<DocumentFolderDto>> findFolders(@RequestBody FindFoldersDto dto) {
        return Result.success(documentSubmissionQueryHandler.findFolders(dto));
    }

    @Override
    @ApiOperation("C.2.4 获取文档（GetDocuments）")
    @PostMapping("/getDocuments")
    public Result<DocumentResultDto<DocumentEntryDto>> getDocuments(@RequestBody GetDocumentsDto dto) {
        return Result.success(documentSubmissionQueryHandler.getDocuments(dto));
    }

    @Override
    @ApiOperation("C.2.5 获取文件夹（GetFolders）")
    @PostMapping("/getFolders")
    public Result<DocumentResultDto<DocumentFolderDto>> getFolders(@RequestBody GetFoldersDto dto) {
        return Result.success(documentSubmissionQueryHandler.getFolders(dto));
    }

    @Override
    @ApiOperation("C.2.6 获取文档以及文档的关联关系（GetDocumentAndAssociations）")
    @PostMapping("/getDocumentAndAssociations")
    public Result<DocumentResultDto<DocumentAssociationDto>> getDocumentAndAssociations(@RequestBody GetDocumentAndAssociationsDto dto) {
        return Result.success(documentSubmissionQueryHandler.getDocumentAndAssociations(dto));
    }

    @Override
    @ApiOperation("C.2.7 获取文档提交集（GetSubmissionSets）")
    @PostMapping("/getSubmissionSets")
    public Result<DocumentResultDto<DocumentSubmissionSetDto>> getSubmissionSets(@RequestBody GetSubmissionSetsDto dto) {
        return Result.success(documentSubmissionQueryHandler.getSubmissionSets(dto));
    }

    @Override
    @ApiOperation("C.2.8 获取文档提交集及内容(GetSubmissionSetAndContents)")
    @PostMapping("/getSubmissionSetAndContents")
    public Result<DocumentResultDto<DocumentSubmissionSetDto>> getSubmissionSetAndContents(@RequestBody GetSubmissionSetAndContentsDto dto) {
        return Result.success(documentSubmissionQueryHandler.getSubmissionSetAndContents(dto));
    }

    @Override
    @ApiOperation("C.2.9 获取文件夹及内容")
    @PostMapping("/getFolderAndContents")
    public Result<DocumentResultDto<DocumentFolderDto>> getFolderAndContents(@RequestBody GetFolderAndContentsDto dto) {
        return Result.success(documentSubmissionQueryHandler.getFolderAndContents(dto));
    }

    @Override
    @ApiOperation("C.2.10 获取文档相关文件夹")
    @PostMapping("/getFoldersForDocument")
    public Result<DocumentResultDto<DocumentFolderDto>> getFoldersForDocument(@RequestBody GetFoldersForDocumentDto dto) {
        return Result.success(documentSubmissionQueryHandler.getFoldersForDocument(dto));
    }

    @Override
    @ApiOperation("C.2.11 获取关联文档")
    @PostMapping("/getRelatedDocuments")
    public Result<DocumentResultDto<DocumentAssociationDto>> getRelatedDocuments(@RequestBody GetRelatedDocumentsDto dto) {
        return Result.success(documentSubmissionQueryHandler.getRelatedDocuments(dto));
    }

    @Override
    @ApiOperation("获取文档内容列表")
    @PostMapping("/getDocumentContentList")
    public Result<List<GetContentRespDto>> getDocumentContentList(@RequestBody GetContentReqDto dto) {
        return Result.success(documentContentQueryHandler.getDocumentContentList(dto));
    }
    
    @ApiOperation(value = "校验结果存储")
    @PostMapping("/saveResult")
    public Result saveResult( @RequestBody DocumentValidateResultDto result) {
    	documentValidataResultServvice.saveResult(result);
        return  Result.success();
    }

    @ApiOperation(value = "下载cda的xml文件")
    @PostMapping("/getDownUrl")
    public Result<String> getDownUrl(@RequestBody List<DocumentValidateResultDto> list) {
        return Result.success(documentValidataResultServvice.getDownURL(list.stream().map(DocumentValidateResultDto::getId).collect(Collectors.toList())));
    }
}
